#include <stdio.h>
#include <stdlib.h>
#include "asusosd_queue.h"

struct _queue_private_{
	unsigned int	*entry;
	int		size;
	int		front;
	int		rear;
};

unsigned int queue_add (queue*, unsigned int);
unsigned int queue_remove (queue*);
unsigned int queue_full (queue*);
unsigned int queue_empty (queue*);

queue*	queue_new(int size)
{
	queue *newq;

	newq = (queue*) malloc(sizeof(queue));
	if(!newq) return NULL;

	newq->info = (queue_private*) malloc(sizeof(queue_private));
	if(!newq->info)
	{
		free(newq);
		return NULL;
	}

	newq->info->entry = (unsigned int *)malloc(sizeof(unsigned short)*size);
	if(!newq->info->entry)
	{
		free(newq->info);
		free(newq);
		return NULL;
	}

	newq->info->size = size;
	newq->info->front = 0;
	newq->info->rear = 0;
	newq->add = queue_add;
	newq->remove = queue_remove;
	newq->isFull = queue_full;
	newq->isEmpty = queue_empty;
	return newq;
}

void	queue_destroy(queue* q)
{
	free(q->info->entry);
	free(q->info);
	free(q);
}

unsigned int queue_add (queue *q, unsigned int bee)
{
	if(q->isFull(q)) return -1;

	q->info->rear = (q->info->rear + 1) % q->info->size;
	q->info->entry[q->info->rear] = bee;
	return 0;
}

unsigned int queue_remove (queue *q)
{
	if(q->isEmpty(q))
		return -1;
	q->info->front = (q->info->front +1) % q->info->size;
	return q->info->entry[q->info->front];
}

unsigned int queue_full (queue* q)
{
	if(q->info->front ==0 && q->info->rear == (q->info->size-1))
		return 1;
	if((q->info->front - q->info->rear) == 1)
		return 1;
	else	return 0;
}

unsigned int queue_empty (queue* q)
{
	if(q->info->rear  == q->info->front)
		return 1;
	else	return 0;
}


